﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using HangfireLearn.Task;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HangfireLearn.Controllers
{
    [Route("")]
    [ApiController]
    public class HangfireController : ControllerBase
    {
        HangfireTask hangfireTask;

        public HangfireController(HangfireTask hangfireTask)
        {
            this.hangfireTask = hangfireTask;
        }

        [HttpGet("t/{count}")]
        public async Task<string> AddTask(int count)
        {
            //后台任务
            await System.Threading.Tasks.Task.Run(() => hangfireTask.StartSimpleJob(count));
           
            return $"加入 {count} 个任务";
        }

        [HttpGet("td/{count}")]
        public async Task<string> AddDelayTask(int count)
        {
            ////延时任务
            await System.Threading.Tasks.Task.Run(() => hangfireTask.StartDelayJob(count));

            return $"加入 {count} 个任务";
        }

        [HttpGet("tr")]
        public string AddRecurringTask()
        {
            //定时（循环）任务代表可以重复性执行多次,这里是每分钟执行一次
            hangfireTask.StartRecurringJob();

            return "OK";
        }
    }


}